package cn.ava.pet.controller;

import cn.ava.basic.exception.BusinessException;
import cn.ava.basic.utils.AjaxResult;
import cn.ava.basic.utils.LoginContext;
import cn.ava.basic.utils.PageList;
import cn.ava.org.domain.Employee;
import cn.ava.org.service.IEmployeeService;
import cn.ava.pet.domain.SearchMasterMsg;
import cn.ava.pet.dto.AcceptOrderDto;
import cn.ava.pet.query.SearchMasterMsgQuery;
import cn.ava.pet.service.ISearchMasterMsgService;
import cn.ava.user.domain.Logininfo;
import cn.ava.user.domain.User;
import cn.ava.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace(); 
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg getById(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping()
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }

    /**
     * @Title: publish
     * @Description: 发布寻主消息
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/19 16:19
     * @param:
     * @return:
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            // 获取到当前登录人信息
            Logininfo loginUser = (Logininfo)LoginContext.getLoginUser(request);
                // 根据登录人查询出用户信息
                User user = userService.getByLogininfoId(loginUser.getId());
                // 设置进发布人用户id中
                searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * @Title: toAudit
     * @Description: 待审核消息查看
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/20 16:52
     * @param:
     * @return:
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query){
            query.setState(0);
            return searchMasterMsgService.queryData(query);
    }
    /**
     * @Title: toHandle
     * @Description: 待处理数据查看
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/20 16:54
     * @param:
     * @return:
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
            Logininfo loginUser =LoginContext.getLoginUser(request);
            if (loginUser != null) {
                Employee employee = employeeService.getByLogininfoId(loginUser.getId());
                query.setShopId(employee.getShopId());
            }
            query.setState(1);
            return searchMasterMsgService.queryData(query);
    }

    /**
     * @Title: pool
     * @Description: 寻主池消息查看
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/20 16:54
     * @param:
     * @return:
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query){
            query.setState(1);
            return searchMasterMsgService.queryData(query);
    }

    /**
     * @Title: finish
     * @Description: 已完成消息查看
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/20 16:54
     * @param:
     * @return:
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query){
            query.setState(2);
            return searchMasterMsgService.queryData(query);
    }
    /**
     * @Title: userSearchMasterMsg
     * @Description: 前台用户查询自己的消息
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/20 17:35
     * @param:
     * @return:
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        Logininfo loginUser = LoginContext.getLoginUser(request);
        if (loginUser != null){
            User user = userService.getByLogininfoId(loginUser.getId());
            searchMasterMsgQuery.setUserId(user.getId());
        }
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }

    /**
     * @Title: reject
     * @Description: 拒绝订单
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/20 19:34
     * @param:
     * @return:
     */
    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id,HttpServletRequest request){
        try {
            searchMasterMsgService.reject(id, request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * @Title: accept
     * @Description: 店铺接单
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/20 21:51
     * @param:
     * @return:
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptOrderDto acceptOrderDto){
        try {
            searchMasterMsgService.accept(acceptOrderDto);
            return new AjaxResult();
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }



}
